<!--author:蒋鑫-->
<!--data:2021/3/3-->
<!--管理自己上传的文件资源-->
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-card-header">
            <h2 class="header-title">资源管理</h2>
            <span class="layui-breadcrumb pull-right">
                 <a href="#!console">首页</a>
                 <a><cite>资源管理</cite></a>
               </span>
        </div>
        <fieldset class="table-search-fieldset" style="border: none">
            <div style="margin: 10px 10px 10px 10px">
                <form action="" class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">资源名称</label>
                            <div class="layui-input-inline">
                                <input autocomplete="off" class="layui-input" name="finame" type="text">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">资源类别</label>
                            <div class="layui-input-inline">
                                <select lay-verify="" name="catgry">
                                    <option>全部</option>
                                    <option value="java">java</option>
                                    <option value="c">c语言</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn" id="resourcesSearch" lay-filter="data-search-btn" lay-submit
                                    type="text"><i
                                    class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <table class="layui-table" id="resources-table" lay-filter="resources-table"></table>
        <!--编辑和删除区域-->
        <script id="resource-table-bar" type="text/html">
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
    </div>
</div>
<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        let form = layui.form;
        let table = layui.table;
        let config = layui.config;
        let layer = layui.layer;
        let util = layui.util;
        let admin = layui.admin;

        // 渲染表格
        table.render({
            elem: '#resources-table',
            url: config.base_server + 'api-forum/resources/selectAllResources',
            method: 'GET',
            where: {
                access_token: config.getToken().access_token,
                userid: config.getUser().id
            },
            page: true,
            cols: [[
                {field: 'fileid', sort: true, title: 'ID', width: 60},
                {field: 'finame', sort: true, title: '资源名称'},
                {field: 'fiinfo', sort: true, title: '资源描述'},
                {field: 'fitype', sort: true, title: '文件类型'},
                {field: 'catgry', sort: true, title: '资源分类'},
                {field: 'fitags', sort: true, title: '资源标签'},
                {field: 'fisize', sort: true, title: '资源大小'},
                {field: 'fcount', sort: true, title: '下载次数'},
                {field: 'points', sort: true, title: '下载积分'},
                {field: 'cretim', sort: true, title: '上传时间'},
                {field: 'updtim', sort: true, title: '更新时间'},
                {align: 'center', toolbar: '#resource-table-bar', title: '操作', width: 150}
            ]]
        });


        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('resources-table', {
                page: {
                    curr: 1//从第一页开始搜索
                }
                , where: data.field

            }, 'data');
            return false;
        });

        // 工具条点击事件
        table.on('tool(resources-table)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                layer.confirm('确定删除此资源吗？', function (index) {
                    admin.req('api-forum/resources/delResources', JSON.stringify(data), function (result) {
                        if (result.code == 0) {
                            layui.table.reload("resources-table");
                            layer.msg(result.msg);
                        } else {
                            layer.msg(result.msg);
                        }

                    }, 'POST');
                });
            }
        });

        //显示表单弹窗
        let showEditModel = function (data) {
            //将表格某一列的值存储起来，用于弹出框的回显
            admin.putTempData('t_resources', data);
            let title = data ? '修改用户' : '添加用户';
            let path='pages/forum/edict_Resource.html';
            admin.popupCenter({
                title: title,
                path: path,
                finish: function () {
                    table.reload('user-table', {});
                }
            });
        };
    });
</script>